package org.jprobe.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.jprobe.Settings;
import org.jprobe.Workspace;
import org.jprobe.utils.UIUtils;
/**
*
* Project view panel
*
* @author Hussain Al-Mutawa
* @version 1.0
*/
@SuppressWarnings("serial")
public abstract class ProjectsViewerPanel extends JPanel {
	/** list of projects in the workspace **/
	private transient final JList list;
	/** the workspace object **/
	private transient final Workspace workspace;
	/**
	 * indicates that the current selected project has been changed
	 * @param project the project directory, full path
	 */
	public abstract void projectChanged(String project);

	/**
	 * initializes new projects viewer panel
	 *
	 * @param settings - the settings controller used by the project
	 */
	public ProjectsViewerPanel(final Settings settings) {
		super();

		workspace = new Workspace();
		final JComboBox text = new JComboBox(new Object[]{"./.."});
		text.setEditable(true);
		list = new JList(new AbstractListModel(){
			@Override public Object getElementAt(final int index) {
				return workspace.getProjects()[index];
			}
			@Override public int getSize() {
				return workspace.getProjects().length;
			}

		});
		list.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent paramListSelectionEvent) {
				projectChanged(workspace.getPath()+'/'+list.getSelectedValue());
			}
		});
		final JScrollPane scrollPane = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		text.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent aev) {
				workspace.setPath((String)text.getSelectedItem());
				list.invalidate();
				list.revalidate();
				list.repaint();
			}
		});
		list.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));

		this.setLayout(new BorderLayout(10,10));
		//this.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		final JPanel container = new JPanel();
		container.setLayout(new BorderLayout(0,2));
		final JTabbedPane tabbed = new JTabbedPane(1);
		container.add(tabbed,BorderLayout.CENTER);
		tabbed.addTab("Workspace projects", scrollPane);

		final JPanel northContainer = new JPanel();
		northContainer.setLayout(new BorderLayout(2,2));
		northContainer.add(text,BorderLayout.CENTER);
		container.add(northContainer,BorderLayout.NORTH);

		this.add(container,BorderLayout.CENTER);
		this.add(northContainer,BorderLayout.NORTH);

		workspace.setPath(settings.getLastDirectory());

		final JButton button = new JButton("Change");
		northContainer.add(button,BorderLayout.EAST);
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent arg0) {
				final String directory = UIUtils.getDirectoy("Select Directory");
				text.setSelectedItem(directory);
				workspace.setPath(directory);
			}
		});
	}
	protected String getCurrentProject() {
		return  workspace.getPath()+'/'+list.getSelectedValue();
	}

}
